<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'status0';

$return['status']=$this->bill_status;
if($_RQ['op']=='status0'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=0;
	if(!empty($_RQ['billno'])){
		$params['billno']=trim($_RQ['billno']);
		$return['params']['billno']=$_RQ['billno'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_globonus_bill',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['date']=$item['year'].'-'.$item['month'];
			if($item['week']){
				$item['date'] .='第'.$item['week'].'周';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'status0','title'=>'待确认'),
			array('op'=>'status1','title'=>'待结算'),
			array('op'=>'status2','title'=>'已结算'),
			array('op'=>'edit','title'=>'创建结算单'),
		),
		'list'=>array('data'=>'list','tip'=>'[平均分红比例]:股东里面有些是等级股东，有些的默认股东。分红比例是不一样的。','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'billno','title'=>'结算单号','type'=>'string'),
			array('name'=>'date','title'=>'日期','type'=>'string'),
			array('name'=>'ordercount','title'=>'订单数量','type'=>'string'),
			array('name'=>'ordermoney','title'=>'订单金额','type'=>'string'),
			array('name'=>'partnercount','title'=>'股东数','type'=>'string'),
			array('name'=>'bonusmoney','title'=>'预计分红','type'=>'string'),
			array('name'=>'bonusrate','title'=>'平均分红比例','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bonusmoney_send','title'=>'最终分红','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>array(0,3))),
		)),
		'search_one'=>array('name'=>'billno','title'=>'结算单号','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$bill_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('model')->check_bill($bill_id,$status,$post['remark']);
		exi('审核成功！','','status0');
	}
	$item=$this->get('bill',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核结算单',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'check_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['check_status']=array(
		'0'=>'待审核',
		'1'=>'审核通过',
		'2'=>'审核不通过',
	);
	exi($return);
}
if($_RQ['op']=='status1'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=1;
	if(!empty($_RQ['billno'])){
		$params['billno']=trim($_RQ['billno']);
		$return['params']['billno']=$_RQ['billno'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_globonus_bill',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['date']=$item['year'].'-'.$item['month'];
			if($item['week']){
				$item['date'] .='第'.$item['week'].'周';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'status0','title'=>'待确认'),
			array('op'=>'status1','title'=>'待结算'),
			array('op'=>'status2','title'=>'已结算'),
			array('op'=>'edit','title'=>'创建结算单'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'billno','title'=>'结算单号','type'=>'string'),
			array('name'=>'date','title'=>'日期','type'=>'string'),
			array('name'=>'ordercount','title'=>'订单数量','type'=>'string'),
			array('name'=>'ordermoney','title'=>'订单金额','type'=>'string'),
			array('name'=>'partnercount','title'=>'股东数','type'=>'string'),
			array('name'=>'bonusmoney','title'=>'预计分红','type'=>'string'),
			array('name'=>'bonusrate','title'=>'平均分红比例','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bonusmoney_send','title'=>'最终分红','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'checkjie','title'=>'结算操作','show'=>array('status'=>1)),
		)),
		'search_one'=>array('name'=>'billno','title'=>'结算单号','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='checkjie'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$bill_id=intval($post['id']);
		$this->model('model')->check_jiebill($bill_id);
		exi('结算成功！','','status1');
	}
	$item=$this->get('bill',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$item['date']=$item['year'].'-'.$item['month'];
	if($item['week']){
		$item['date'] .='第'.$item['week'].'周';
	}
	$item['withdrawtype']=$this->withdraw[$this->setting['withdrawtype']];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'确认算单',
		'edit'=>array(
			array('name'=>'billno','title'=>'结算单号','type'=>'copy'),
			array('name'=>'date','title'=>'日期','type'=>'copy'),
			array('name'=>'ordercount','title'=>'订单数量','type'=>'copy'),
			array('name'=>'ordermoney','title'=>'订单金额','type'=>'copy'),
			array('name'=>'partnercount','title'=>'股东数','type'=>'copy'),
			array('name'=>'bonusmoney','title'=>'预计分红','type'=>'copy'),
			array('name'=>'bonusmoney_send','title'=>'最终分红','type'=>'copy'),
			array('name'=>'withdrawtype','title'=>'打款方式','type'=>'copy'),
		),
	);
	exi($return);
}
if($_RQ['op']=='status2'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=2;
	if(!empty($_RQ['billno'])){
		$params['billno']=trim($_RQ['billno']);
		$return['params']['billno']=$_RQ['billno'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_globonus_bill',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['date']=$item['year'].'-'.$item['month'];
			if($item['week']){
				$item['date'] .='第'.$item['week'].'周';
			}
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'status0','title'=>'待确认'),
			array('op'=>'status1','title'=>'待结算'),
			array('op'=>'status2','title'=>'已结算'),
			array('op'=>'edit','title'=>'创建结算单'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'billno','title'=>'结算单号','type'=>'string'),
			array('name'=>'date','title'=>'日期','type'=>'string'),
			array('name'=>'ordercount','title'=>'订单数量','type'=>'string'),
			array('name'=>'ordermoney','title'=>'订单金额','type'=>'string'),
			array('name'=>'partnercount','title'=>'股东数','type'=>'string'),
			array('name'=>'bonusmoney','title'=>'预计分红','type'=>'string'),
			array('name'=>'bonusrate','title'=>'平均分红比例','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bonusmoney_send','title'=>'最终分红','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			
		)),
		'search_one'=>array('name'=>'billno','title'=>'结算单号','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$setting=$this->plugin_setting('setting');
	$year=intval(date('Y',time()));
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['week']=array(
		array('id'=>1,'title'=>'第一周'),
		array('id'=>2,'title'=>'第二周'),
		array('id'=>3,'title'=>'第三周'),
		array('id'=>4,'title'=>'第四周')
	);	
	if($setting['zhouqi']==2){
		$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'tabs'=>array(
				array('op'=>'status0','title'=>'结算单'),
			),
			'edit'=>array(
				array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
				array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
				array('name'=>'week','title'=>'选择周数','type'=>'select','options'=>'week'),
			)
		);
		$return['htmls']=$htmls;
		exi($return);
	}else{
		$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'tabs'=>array(
				array('op'=>'status0','title'=>'结算单'),
			),
			'edit'=>array(
				array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
				array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
			)
		);
		$return['htmls']=$htmls;
		exi($return);
	}
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['year']){
		exi('请选择年份','error');
	}
	if(!$postdata['month']){
		exi('请选择月份','error');
	} 
	if($postdata['month']<10){
		$postdata['month']='0'.$postdata['month'];
	}
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'year'=>$postdata['year'],'month'=>$postdata['month']);
	if(!$postdata['week']){
		$firstday = strtotime($postdata['year'] .$postdata['month'] . "01000000");
		$firstdaystr = date("Y-m-01", $firstday);
		$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
	}else{
		if($postdata['week']<4){
			$day=($postdata['week']-1)*7+1;
			if($day<10){
				$day='0'.$day;
			}
			$firstday = strtotime($postdata['year'] .$postdata['month'] .$day. "000000");
			$firstdaystr = date("Y-m-d", $firstday);
			$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr  +6 day")));
		}else{
			$firstday = strtotime($postdata['year'] .$postdata['month'] . "22000000");
			$firstdaystr = date("Y-m-01", $firstday);
			$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
		}
		$params['week']=$postdata['week'];
	}
	if($lastday>time()){
		exi('当天结算周期还未结束！不能提交结算！','error');
	}
	$isadd=pdo_get('jy_globonus_bill',$params);
	if($isadd['id']){
		$date=$postdata['year'].'-'.$postdata['month'];
		if($postdata['week']){
			$date .='第'.$postdata['week'].'周';
		}
		exi($date.'已提交结算，不可重复提交！','error');
	}
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'ispartner'=>'1','partnerstatus'=>1,'partnertime <'=>$lastday);
	$partnercount=pdo_count('jy_weishop_member',$params,'mid');
	$orders=pdo_getall('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime between'=>array($firstday,$lastday)));
	$money=0;
	$bonusmoney=0;
	$total=count($orders);
	if(!$total){
		exi('没有可结算订单','error');
	}
	foreach($orders as $ov){
		$money +=$ov['price'];
		$onered=$this->model('model')->get_redfee($ov);
		$bonusmoney +=$onered;
	}
	$bonusrate=intval(100*$bonusmoney/$money);
	$item['year']=$postdata['year'];
	$item['month']=$postdata['month'];
	$item['starttime']=$firstday;
	$item['endtime']=$lastday;
	$item['week']=$postdata['week'];
	$item['partnercount']=$partnercount;
	$item['ordercount']=$total?:0;
	$item['ordermoney']=intval($money*100)/100;
	$item['bonusmoney']=intval($bonusmoney*100)/100;
	$item['bonusrate']=$bonusrate.'%';
	$item['bonusmoney_send']=$item['bonusmoney'];
	$this->session('item',$item);
	exi('','','step1');
}
if($_RQ['op']=='step1'){
	$return['step']=1;
	$item=$this->session('item');
	$item['week2']=$item['week'];
	$item['year2']=$item['year'];
	$return['item']=$item;
	$htmls=array(
				'type'=>'edit',
				'post'=>'do',
				'edit'=>array(
					array('name'=>'year2','title'=>'年份','type'=>'string','read'=>true),
					array('name'=>'month','title'=>'月份','type'=>'string','read'=>true),
					array('name'=>'week2','title'=>'周','type'=>'string','read'=>true,'show'=>array('week2 >'=>0)),
					array('name'=>'ordercount','title'=>'订单数量','type'=>'string','read'=>true),
					array('name'=>'ordermoney','title'=>'订单金额','type'=>'string','read'=>true),
					array('name'=>'partnercount','title'=>'股东数量','type'=>'string','read'=>true),
					array('name'=>'bonusrate','title'=>'平均分红比例','type'=>'string','read'=>true),
					array('name'=>'bonusmoney','title'=>'预计分红','type'=>'string','read'=>true),
					array('name'=>'bonusmoney_send','title'=>'最终分红','type'=>'string','help'=>'如果您的最终分红和预计分红不一致，则实际给股东的分红会按照预计分红及最终分红比例进行分红'),
				)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='do'){
	$postdata=get_postdata();
	$data=$postdata;
	$data['uniacid']=$_SESSION['uniacid'];
	$data['merchid']=$_SESSION['onemerchid'];
	$data['createtime']=time();
	$data['year']=$data['year2'];
	$data['week']=$data['week2'];
	$data['billno']=$data['year'].$data['month'].rand(1000,9999);	
	unset($data['year2']);
	unset($data['week2']);
	pdo_insert('jy_globonus_bill',$data);
	exi('创建结算单成功','','status0');
}
